# -*- coding: utf8 -*-
# Written by koya
#image.py

from OpenGL.GL import *

import os

import pygame
from pygame.locals import *

import font

# 载入图片, 需指定文件名, 路径可选 
def load_image( filename, path = "data", trans = True ):

      img = pygame.image.load(os.path.join(path, filename))

      if trans:
            return img.convert_alpha()
      else:
            return img.convert()


# 根据surface来截图
def save_image( surface ):
    
      number = 1
      filename = "screenshot%d.bmp" % number
      
      while os.path.exists( filename ):

          number += 1
          filename = "screenshot%d.bmp" % n
            
      pygame.image.save(surface, filename)

def pow2(x):
    y = 1
    while y < x:
        y <<= 1
    return y

def load_texture2( filename ):

    img = pygame.image.load(filename)
    width = img.get_width()
    height = img.get_height()
    texwidth = pow2(width)
    texheight = pow2(height)
    if texwidth != width or texheight != height:

        data = chr(0) * texwidth* texheight*4
        surf = pygame.image.fromstring(data,(texwidth, texheight), 'RGBA')
        surf.blit(img, (0,texheight-height))
        img = surf
    u, v = width / float(texwidth), height/float(texheight)
    tex = glGenTextures(1)
    glBindTexture(GL_TEXTURE_2D, tex)
    glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
    glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
    glTexImage2D(GL_TEXTURE_2D, 0, 4, texwidth, texheight,0, GL_RGBA, GL_UNSIGNED_BYTE,\
                         pygame.image.tostring(img, 'RGBA', True))

    return tex
            
            

def load_texture( filename ):
    #global texture
    image_data = pygame.image.load(filename)#.convert_alpha()
    #image_data.set_colorkey( (0, 0, 0), RLEACCEL )
            
    ix = image_data.get_width()
    iy = image_data.get_height()

    image = pygame.image.tostring( image_data, "RGBA", 1)
            
    # Create Texture
    Texid = glGenTextures(1)
    glBindTexture(GL_TEXTURE_2D, Texid)              
    glPixelStorei(GL_UNPACK_ALIGNMENT,1)
        
    #glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP)
    #glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP)
    #glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
    #glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL)
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ix, iy, 0, GL_RGBA, GL_UNSIGNED_BYTE, image)

    return Texid
def load_image_to_texture( image_data ):
    #global texture
            
    ix = image_data.get_width()
    iy = image_data.get_height()

    image = pygame.image.tostring( image_data, "RGBA", 1)
            
    # Create Texture
    Texid = glGenTextures(1)
    glBindTexture(GL_TEXTURE_2D, Texid)              
    glPixelStorei(GL_UNPACK_ALIGNMENT,1)
        
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL)
    glTexImage2D(GL_TEXTURE_2D, 0, 3, ix, iy, 0, GL_RGBA, GL_UNSIGNED_BYTE, image)

    return Texid

def change_font_to_texture( msg, antialias = True, color = (255,255,255), background = None ):

    texid = glGenTextures(1)

    font_tex  = font.load_font( "Hemi Head 426.TTF", 50 )
    texture = font_tex.render( str(msg), antialias, color )
    texture.convert_alpha()
    
    
    width = texture.get_width()
    height = texture.get_height()
    texwidth = pow2(width)
    texheight = pow2(height)
    if texwidth != width or texheight != height:

        data = chr(0) * texwidth* texheight*4
        surf = pygame.image.fromstring(data,(texwidth, texheight), 'RGBA')
        surf.blit(texture, (0,texheight-height))
        texture = surf
    u, v = width / float(texwidth), height/float(texheight)
    tex = glGenTextures(1)
    glBindTexture(GL_TEXTURE_2D, tex)
    glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
    glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
    glTexImage2D(GL_TEXTURE_2D, 0, 4, texwidth, texheight,0, GL_RGBA, GL_UNSIGNED_BYTE,\
                         pygame.image.tostring(texture, 'RGBA', True))

    return tex

"""def change_font_to_texture( msg, antialias = True, color = (255,255,255), background = None ):

    texid = glGenTextures(1)

    font_tex  = font.load_font( "Hemi Head 426.TTF", 100 )
    texture = font_tex.render( str(msg), antialias, color )


    texture_data = pygame.image.tostring( texture, "RGBA", 1 )

    glBindTexture(GL_TEXTURE_2D, texid) 
    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, texture.get_width(), texture.get_height(), 0,
                  GL_RGBA, GL_UNSIGNED_BYTE, texture_data )

    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
   
    return texid"""
